#include "qresourcefilemodel.h"

#include "../../include/abstractcore.h"
#include "../../include/resourceinterface.h"

#include <QModelIndex>
#include <QIcon>

QResourceFileModel::QResourceFileModel(AbstractCore *core, QObject *parent):
    QAbstractListModel(parent),
    m_core(core)
{
}

QVariant QResourceFileModel::data(const QModelIndex &index, int role) const
{
    const int row = index.row();
    if (row < 0 || row >=  m_resourceInfos.size())
        return QVariant();

    const tagResourceInfo &item = m_resourceInfos.at(row);
    switch (role) {
    case Qt::DisplayRole:
        return QVariant(item.m_name);
    case Qt::DecorationRole:
    {
        return QVariant(item.m_icon);
    }
    case Qt::EditRole:
        return QVariant(item.m_name);
    case Qt::ToolTipRole:
        return QVariant(item.m_name);
    case Qt::UserRole:
        return QVariant(item.m_bSelect);
    }
    return QVariant();
}

bool QResourceFileModel::setData(const QModelIndex &index, const QVariant &value, int role)
{

    const int row = index.row();
    if (row < 0 || row >=  m_resourceInfos.size())
        return false;

    tagResourceInfo item = m_resourceInfos.at(row);
    switch(role)
    {
    case Qt::UserRole:
        item.m_bSelect=value.toBool();
        break;
    default:
        return true;
    }

    m_resourceInfos.replace(row,item);
    emit dataChanged(index, index);
    return true;
}

Qt::ItemFlags QResourceFileModel::flags(const QModelIndex &/*index*/) const
{
    Qt::ItemFlags rc = Qt::ItemIsEnabled;
    return rc;
}

int QResourceFileModel::rowCount(const QModelIndex & /*parent*/) const
{
    return m_resourceInfos.size();
}

void QResourceFileModel::addFile(QString name)
{
    QString n=name;
    int index=name.indexOf("/");
    if(index>=0)
    {
        name=name.mid(index+1);
    }
    if(findResource(name)>=0)
    {
        return;
    }

    tagResourceInfo info;
    info.m_bSelect=false;
    info.m_name=name;
    if(index<0)
    {
        info.m_icon=QIcon(":/images/resource_toplevel.png");
    }
    else
    {
        QString tp=n.left(index);
        QResourceInterface *res=m_core->resourceInterface();
        QByteArray array=res->getResourceData(tp,name);
        QPixmap p;
        p.loadFromData(array);
        info.m_icon=QIcon(p);
    }

    const int row = m_resourceInfos.size();
    beginInsertRows(QModelIndex(), row, row);
    m_resourceInfos.append(info);
    endInsertRows();
}

void QResourceFileModel::removeFile(QString name)
{
    int index=findResource(name);
    if(index<0)
    {
        return;
    }

    beginRemoveRows(QModelIndex(),index,index);
    m_resourceInfos.removeAt(index);
    endRemoveRows();
}

int QResourceFileModel::findResource(QString name)
{
    for(int i=0;i<m_resourceInfos.size();i++)
    {
        if(m_resourceInfos.at(i).m_name==name)
        {
            return i;
        }
    }
    return -1;
}

QString QResourceFileModel::select()
{
    for(int i=0;i<m_resourceInfos.size();i++)
    {
        if(m_resourceInfos.at(i).m_bSelect)
        {
            return m_resourceInfos.at(i).m_name;
        }
    }
    return "";
}

void QResourceFileModel::clear()
{
    beginRemoveRows(QModelIndex(),0,m_resourceInfos.size());
    m_resourceInfos.clear();
    endRemoveRows();
}
